package com.qj.yantaishan.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qj.yantaishan.common.utils.TimeMyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.qj.yantaishan.mapper.FinancialRecordMapper;
import com.qj.yantaishan.domain.FinancialRecord;
import com.qj.yantaishan.service.IFinancialRecordService;
import com.qj.yantaishan.common.core.text.Convert;

/**
 * 财务记录Service业务层处理
 *
 * @author qihe
 * @date 2022-07-22
 */
@Service
@Primary
public class FinancialRecordServiceImpl extends ServiceImpl<FinancialRecordMapper, FinancialRecord> implements IFinancialRecordService {
    @Autowired
    private FinancialRecordMapper financialRecordMapper;

    /**
     * 查询财务记录
     *
     * @param pigcmsId 财务记录主键
     * @return 财务记录
     */
    @Override
    public FinancialRecord selectFinancialRecordByPigcmsId(Long pigcmsId) {
        return financialRecordMapper.selectFinancialRecordByPigcmsId(pigcmsId);
    }

    /**
     * 查询财务记录列表
     *
     * @param financialRecord 财务记录
     * @return 财务记录
     */
    @Override
    public List<FinancialRecord> selectFinancialRecordList(FinancialRecord financialRecord) {
        return financialRecordMapper.selectFinancialRecordList(financialRecord);
    }

    /**
     * 新增财务记录
     *
     * @param financialRecord 财务记录
     * @return 结果
     */
    @Override
    public int insertFinancialRecord(FinancialRecord financialRecord) {
        return financialRecordMapper.insertFinancialRecord(financialRecord);
    }

    /**
     * 修改财务记录
     *
     * @param financialRecord 财务记录
     * @return 结果
     */
    @Override
    public int updateFinancialRecord(FinancialRecord financialRecord) {
        return financialRecordMapper.updateFinancialRecord(financialRecord);
    }

    /**
     * 批量删除财务记录
     *
     * @param pigcmsIds 需要删除的财务记录主键
     * @return 结果
     */
    @Override
    public int deleteFinancialRecordByPigcmsIds(String pigcmsIds) {
        return financialRecordMapper.deleteFinancialRecordByPigcmsIds(Convert.toStrArray(pigcmsIds));
    }

    /**
     * 删除财务记录信息
     *
     * @param pigcmsId 财务记录主键
     * @return 结果
     */
    @Override
    public int deleteFinancialRecordByPigcmsId(Long pigcmsId) {
        return financialRecordMapper.deleteFinancialRecordByPigcmsId(pigcmsId);
    }

    @Override
    public Map<String, Object> selectPlatform() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> store = new HashMap<>();
        /**
         * 店铺信息
         *
         */
        store = financialRecordMapper.selectStoreMap();
        map.put("store", store);
        /**
         * 累计收款
         * select sum(if(income<0,0,income)) from shanglala_financial_record
         totalGetMoney
         */
        BigDecimal totalGetMoney = financialRecordMapper.selectTotalGetMoney();
        map.put("totalGetMoney", totalGetMoney);

        /**
         * 退款金额
         * select * from shanglala_financial_record sfr left join shanglala_order so on sfr.order_id = so.order_id where sfr.income<0 and sfr.type=3  and is_agent=1
         totalReturnMoney
         */
        BigDecimal totalReturnMoney = financialRecordMapper.selectTotalReturnMoney();
        map.put("totalReturnMoney", totalReturnMoney);

        /**
         * 可提现金额
         * select unbalance from shanglala_store
         */
        BigDecimal totalBalance = financialRecordMapper.selectTotalBalance();
        map.put("totalBalance", totalBalance);

        /**
         * 待结算金额
         * select sum(sfr.income) from shanglala_financial_record sfr left join shanglala_order so on sfr.order_id = so.order_id where sfr.type in(1,6) and so.is_agent=1 and so.status in(2,3)
         */
        BigDecimal totalIncome = financialRecordMapper.selectTotalIncome();
        map.put("totalIncome", totalIncome);

        //7日收入统计(截止今日)
        List<Map<String, Object>> day7List = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            Map<String, Long> startAndEndTime = TimeMyUtil.getStartAndEndTime(new Date().getTime() / 1000, -i);
            Map<String, Object> day7 = new HashMap<>();
            Long startTime = startAndEndTime.get("startTime");
            Long endTime = startAndEndTime.get("endTime");
            //提现
            BigDecimal nowWithdrawMoney = financialRecordMapper.selectWithdrawByTime(startTime, endTime);
            //退款
            BigDecimal nowReturnMoney = financialRecordMapper.selectReturnByTime(startTime, endTime);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            final String format = dateFormat.format(new Date(startTime * 1000));
            day7.put("nowWithdrawMoney", nowWithdrawMoney);
            day7.put("nowReturnMoney", nowReturnMoney);
            day7.put("nowSubMoney", 0);
            day7.put("date", format);
            day7List.add(day7);

        }
        map.put("day7List", day7List);

        return map;
    }

    /**
     * 交易记录列表
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> selectIncome(Integer currentPage, Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        int current = (currentPage - 1) * pageSize;
        int total = financialRecordMapper.selectIncomeTotal();
        int pages = (total + currentPage - 1) / pageSize;
        result.put("current", currentPage);
        result.put("total", total);
        result.put("pages", pages);
        result.put("pageSize", pageSize);
        List<Map<String, Object>> list = financialRecordMapper.selectIncomeList(current, pageSize);
        for (Map<String, Object> map : list) {
            Object orderNo = map.get("order_no");
            BigDecimal bigDecimal = financialRecordMapper.selectIncomeByOrderNo(orderNo);
            map.put("actualIncome", bigDecimal);
        }
        result.put("list", list);
        return result;
    }

    @Override
    public Map<String, Object> selectInoutdetailList(Integer currentPage, Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        int current = (currentPage - 1) * pageSize;
        int total = financialRecordMapper.selectInoutdetailTotal();
        int pages = (total + currentPage - 1) / pageSize;
        result.put("current", currentPage);
        result.put("total", total);
        result.put("pages", pages);
        result.put("pageSize", pageSize);
        List<Map<String, Object>> list = financialRecordMapper.selectInoutdetailList(current, pageSize);
        result.put("list", list);
        return result;

    }

    @Override
    public Map<String, Object> selectWithdrawList(Integer currentPage, Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        int current = (currentPage - 1) * pageSize;
        int total = financialRecordMapper.selectWithdrawTotal();
        int pages = (total + currentPage - 1) / pageSize;
        result.put("current", currentPage);
        result.put("total", total);
        result.put("pages", pages);
        result.put("pageSize", pageSize);
        List<Map<String, Object>> list = financialRecordMapper.selectWithdrawList(current, pageSize);
        result.put("list", list);
        return result;
    }
}
